package com.lalosoft.twitter.config;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import winterwell.jtwitter.OAuthSignpostClient;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

public class OAuthHelper {

	private static String TAG = "OAuthHelper";
	
	private OAuthSignpostClient oauthClient;   
	private CommonsHttpOAuthConsumer commonHttpOAuthConsumer;
	private OAuthConsumer mConsumer;
	private OAuthProvider mProvider;
	private static final String PREFS_NAME = "TWITTERDEMOANDROID";
	private String username = "";
	private String oauthKey = "";
	private String oauthSecret = "";
	private String accessToken = "";
	private String accessTokenSecret = "";
	private Context context;
	
	private SharedPreferences settings;
	    
	private static OAuthHelper instance;
	
	public static OAuthHelper getInstance(Context ctx){
		if(instance == null){
			instance = new OAuthHelper(ctx);
		}
		return instance;
	}
	    
	private OAuthHelper(Context ctx){
		context = ctx;
		load();
        mProvider = new DefaultOAuthProvider(
                "https://api.twitter.com/oauth/request_token",
                "https://api.twitter.com/oauth/access_token",
                "https://api.twitter.com/oauth/authorize");
        
	}
	
	private void load(){
		settings = context.getSharedPreferences(PREFS_NAME, 0);
		this.accessToken = settings.getString(Constants.ACCESS_TOKEN, "");
		this.accessTokenSecret = settings.getString(Constants.ACCESS_TOKEN_SECRET, "");
		this.oauthKey = settings.getString(Constants.OAUTH_KEY, "");
		this.oauthSecret = settings.getString(Constants.OAUTH_SECRET, "");
		this.username = settings.getString(Constants.USERNAME, "");
	}
	
	public void save(){
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(Constants.ACCESS_TOKEN, this.accessToken);
		editor.putString(Constants.ACCESS_TOKEN_SECRET, this.accessTokenSecret);
		editor.putString(Constants.OAUTH_KEY, this.oauthKey);
		editor.putString(Constants.OAUTH_SECRET, this.oauthSecret);
		editor.putString(Constants.USERNAME, this.username);
		editor.commit();
		update();
	}
	
	private void update(){
		mConsumer = new CommonsHttpOAuthConsumer(oauthKey, oauthSecret);
		mConsumer.setTokenWithSecret(accessToken, accessTokenSecret);
	}
	
	public void setUserName(String username){
		this.username = username;
	}
	
	public String getUserName(){
		return this.username;
	}
	
	public OAuthSignpostClient getOAuthClient(){
		if(oauthClient == null){
//			setOAuthKey();
			oauthKey = getResourceCode("oauth/oauth_key");
			oauthSecret = getResourceCode("oauth/oauth_secret");
			accessToken = getResourceCode("token/token");
			accessTokenSecret = getResourceCode("token/token_secret");
			oauthClient = new OAuthSignpostClient(oauthKey, oauthSecret, accessToken, accessTokenSecret);	
//			commonHttpOAuthConsumer = new CommonsHttpOAuthConsumer(oauthKey, oauthSecret);
		}
		return oauthClient;
	}

	public OAuthConsumer getmConsumer() {
		return mConsumer;
	}

	public void setmConsumer(OAuthConsumer mConsumer) {
		this.mConsumer = mConsumer;
	}

	public OAuthProvider getmProvider() {
		return mProvider;
	}

	public void setmProvider(OAuthProvider mProvider) {
		this.mProvider = mProvider;
	}

	public String getOauthKey() {
		return oauthKey;
	}

	public String getOauthSecret() {
		return oauthSecret;
	}

	public void setOauthSecret(String oauthSecret) {
		this.oauthSecret = oauthSecret;
	}

	public String getAccessToken() {
		return accessToken;
	}

	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}

	public String getAccessTokenSecret() {
		return accessTokenSecret;
	}

	public void setAccessTokenSecret(String accessTokenSecret) {
		this.accessTokenSecret = accessTokenSecret;
	}
	
	private String getResourceCode(String assetsPath){
		String code = null;
		try {
			InputStream is = context.getAssets().open(assetsPath);
			BufferedReader reader = new BufferedReader(new InputStreamReader(is));
			code = reader.readLine();
		} catch (IOException e1) {
			Log.e(TAG, "Error: " + e1.getMessage());
			e1.printStackTrace();
		}
		
		return code;
	}
//	
//	private void setOAuthKey(){
//		try {
//			InputStream is = context.getAssets().open("oauth/oauth_key");
//			BufferedReader reader = new BufferedReader(new InputStreamReader(is));
//			String oauthKey = null;
//			oauthKey = reader.readLine();
//			this.oauthKey = oauthKey;
//		} catch (IOException e1) {
//			Log.e(TAG, "Error: " + e1.getMessage());
//			e1.printStackTrace();
//		}
//		
//		Log.i(TAG, "oauthKey = " + oauthKey);
//	}
	
	
}
